[HEADER]
	VERSION		00.00.00.00
	DESCRIPTION Environment Map Example
	COPYRIGHT	Imagination Technologies Ltd.
[/HEADER]

[TEXTURES]
	FILE base 			base.pvr			LINEAR-LINEAR-LINEAR
	FILE reflection 	reflection.pvr		LINEAR-LINEAR-LINEAR
[/TEXTURES]

[VERTEXSHADER]
	NAME myVertShader
	[GLSL_CODE]
		attribute highp vec4	myVertex;
		attribute mediump vec3	myNormal;
		attribute mediump vec4	myUV;
		uniform mediump mat4	myMVPMatrix;
		uniform mediump mat3	myModelViewIT;

		mediump vec3  Normal;
		mediump vec3  EyeDir;
		
		varying mediump float LightIntensity;
		varying mediump vec2  texCoord;
		varying mediump vec2  ReflectTexCoord;

		const mediump vec3  LightPos = vec3(1,1,1);
		const mediump vec3 Xunitvec = vec3 (1.0, 0.0, 0.0);
		const mediump vec3 Yunitvec = vec3 (0.0, 1.0, 0.0);

		void main(void) 
		{
			// Transform Position
			gl_Position = myMVPMatrix * myVertex;
			
			// Transform Normal
			Normal = myModelViewIT * myNormal;
				
			// Calculate diffuse lighting
			EyeDir         = vec3(gl_Position);
			LightIntensity = abs(dot(normalize(LightPos - EyeDir), Normal))*0.5+0.5;
			
			// Passtrough UVs
			texCoord = myUV.st;
			
			// Compute reflection vector   
			mediump vec3 reflectDir = normalize(reflect(EyeDir, Normal));

			// Compute altitude and azimuth angles
			ReflectTexCoord.y = dot(reflectDir, Yunitvec);
			ReflectTexCoord.x = dot(reflectDir, Xunitvec);

			ReflectTexCoord = (ReflectTexCoord + 1.0) * 0.5;

		}
	[/GLSL_CODE]
[/VERTEXSHADER]

[FRAGMENTSHADER]
	NAME myFragShader
	[GLSL_CODE]


		const mediump vec3  BaseColor = vec3(0.5, 1.0, 0.8);
		const mediump float MixRatio = 0.6;

		uniform sampler2D sampler2d;
		uniform sampler2D reflect2d;

		varying mediump float LightIntensity;
		varying mediump vec2  texCoord;
		varying mediump vec2  ReflectTexCoord;

		void main (void)
		{

			// Draw the static base    
			mediump vec3 envColor  = vec3 (texture2D(sampler2d, texCoord));
		    
			// Do a lookup into the environment map.
			mediump vec3 envColor2 = vec3 (texture2D(reflect2d, ReflectTexCoord));
			
			// Mix them
			envColor = mix(envColor, envColor2, MixRatio);

			// Add lighting to base color and mix
			mediump vec3 base = LightIntensity * BaseColor;
			envColor = mix(envColor, base, MixRatio);

			gl_FragColor = vec4 (envColor, 1.0);
		}
	[/GLSL_CODE]
[/FRAGMENTSHADER]

[EFFECT]
	NAME myEffect

	ATTRIBUTE	myVertex			POSITION
	ATTRIBUTE	myNormal			NORMAL
	ATTRIBUTE	myUV				UV
	UNIFORM		myMVPMatrix			WORLDVIEWPROJECTION
	UNIFORM		myModelViewIT		WORLDVIEWIT
	UNIFORM		sampler2d			TEXTURE0
	UNIFORM		reflect2d			TEXTURE1

	TEXTURE 0 base
	TEXTURE 1 reflection

	VERTEXSHADER myVertShader
	FRAGMENTSHADER myFragShader
[/EFFECT]
